package com.example.administrator.chinafood;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.WindowDecorActionBar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.chinafood.SQLite.MyDBOpenHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyForum extends AppCompatActivity implements View.OnClickListener{

    private EditText ed_title;
    private EditText ed_content;
    private Button btn_clear;
    private Button btn_add;
    private MyDBOpenHelper myDBOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_forum);

        ed_title = (EditText)findViewById(R.id.myforum_title);
        ed_content = (EditText)findViewById(R.id.myforum_content);
        btn_clear = (Button)findViewById(R.id.myforum_btn_clear);
        btn_add = (Button)findViewById(R.id.myforum_btn_add);
        myDBOpenHelper = new MyDBOpenHelper(MyForum.this,"ChinaFood.db");

        btn_clear.setOnClickListener(this);
        btn_add.setOnClickListener(this);
    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.myforum_btn_clear:
                btnClear();
                break;
            case R.id.myforum_btn_add:
                btnAdd();
                break;
        }
    }

    private void btnClear(){
        ed_title.setText("");
        ed_content.setText("");
    }

    private void btnAdd(){
        SQLiteDatabase db = myDBOpenHelper.getWritableDatabase();

        String title = ed_title.getText().toString().trim();
        String content = ed_content.getText().toString().trim();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        String time = sdf.format(new Date().getTime());
        String author = "author1";
        Integer img = R.drawable.img_0;
        String num = "100";
        Object []input = {title,author,time,content,img,num};
        db.execSQL("insert into forum (title,author,time,content,img,num) values(?,?,?,?,?,?)",input);
        Toast.makeText(MyForum.this,"添加成功",Toast.LENGTH_SHORT).show();
        db.close();
    }
}
